<?php
    /**
    * Handle all donwload requests to download any data from clients data
    * @author trungdt
    * @since Jan 31, 2013
    * @version 1.0
    */
    if (!defined('EXPONENT')) exit('');
    global $user;
    /**
    * Input params
    Array
    (
    [username] => root
    [filename] => 1359547711_Contrat
    [file_ext] => pdf
    [module] => profilemodule
    [action] => download
    [ajax_action] => 1
    )
    */
    if(exponent_users_isLoggedIn() && isset($_REQUEST['filename']) && isset($_REQUEST['file_ext']) && isset($_REQUEST['username']))
    {
        $file_name = _to_vietnamese_with_nomark($_REQUEST['filename']);
        $file_ext = _to_vietnamese_with_nomark($_REQUEST['file_ext']);
        $username = _to_vietnamese_with_nomark($_REQUEST['username']);

        global $user;
        if ($user->username == $username || exponent_users_isAdmin())
        {
            //$mime_type = (EXPONENT_USER_BROWSER == 'IE' || EXPONENT_USER_BROWSER == 'OPERA') ? 'application/octet-stream;' :  $file->mimetype;
            
            $file_path = BASE."files/profiles/{$username}/{$file_name}.{$file_ext}";
            
            //$mime_type=mime_content_type($file_path );
            $finfo = finfo_open(FILEINFO_MIME); 
            $mime_type=finfo_file($finfo, $file_path) ;
            
            header('Content-Type: ' . $mime_type);
            header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
            header("Content-length: ".filesize($file_path));
            header('Content-Transfer-Encoding: binary');
            header('Content-Disposition: attachment; filename="'.$file_name.".".$file_ext. '"');
            // IE need specific headers
            if (EXPONENT_USER_BROWSER == 'IE') {
                header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                header('Pragma: public');
                header('Vary: User-Agent');
            } else {
                header('Pragma: no-cache');
            }
            
            //Read the file out directly
            readfile($file_path);
            exit();
        }
        else
            exponent_http_error(403);
    }
    else
        exponent_http_error(403);
?>
